﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Dapper;
using System.Threading.Tasks;

namespace Lifang.Dao
{
    public class DapperHelper
    {
        /// <summary>  
        /// DB Connetction String  
        /// </summary>  
        private static string connectionString { get; set; }
        /// <summary>  
        /// Get Entity (int key)  
        /// </summary>   
        public static async Task<T> GetAsync<T>(int id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.GetAsync<T>(id, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        /// Get Entity (long key)  
        /// </summary>   
        public static async Task<T> GetAsync<T>(long id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.QueryAsync<T>(id, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        /// Get Entity (guid key)  
        /// </summary>   
        public static async Task<T> GetAsync<T>(System.Guid id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.GetAsync<T>(id, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        /// Get Entity (string key)  
        /// </summary>   
        public static async Task<T> GetAsync<T>(string id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.QueryAsync<T>(id, transaction);
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        /// Get All List  
        /// </summary>   
        public static async Task<IEnumerable<T>> GetAllAsync<T>() where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.GetAllAsync<T>();
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        /// Get List With SQL  
        /// </summary>   
        public static async Task<IEnumerable<T>> GetListAsync<T>(string sql) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.QueryAsync<T>(sql, commandType: CommandType.Text);
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        /// Insert Entity  
        /// </summary>  
        public static async Task<int> InsertAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.InsertAsync<T>(model, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                
                return 0;
            }
        }
        /// <summary>  
        /// Update Entity  
        /// </summary>  
        public static async Task<T> UpdateAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    bool b = await conn.UpdateAsync<T>(model, transaction, commandTimeout);
                    if (b) { return model; }
                    else { return null; }
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        /// Delete Entity  
        /// </summary>  
        public static async Task<T> DeleteAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    bool b = await conn.DeleteAsync<T>(model, transaction, commandTimeout);
                    if (b) { return model; }
                    else { return null; }
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        /// <summary>  
        ///Execute SQL Statement  
        /// </summary>   
        public static async Task<int> ExecSqlAsync<T>(string sql)
        {
            try
            {
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.ExecuteAsync(sql);
                }
            }
            catch (Exception ex)
            {
                
                return 0;
            }
        }

        #region For Project  
        /// <summary>  
        /// Get Entity By ProcInstID  
        /// </summary>   
        public static async Task<T> GetByProcInstIdAsync<T>(string procInstId) where T : class, new()
        {
            try
            {
                string tbname = typeof(T).Name;
                string sql = string.Format("SELECT * FROM {0} WHERE ProcInstID='{1}'", tbname, procInstId);
                using (var conn = new MySqlConnection(connectionString))
                {
                    return await conn.QueryFirstOrDefaultAsync<T>(sql);
                }
            }
            catch (Exception ex)
            {
                
                return null;
            }
        }
        #endregion
    }
}
